﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Agile.Service;
using Web.Models.Settings;

namespace Web.Factories
{
    public partial interface ISettingModelFactory
    {
        GeneralCommonSettingsModel PrepareGeneralCommonSettingsModel();
    }
    public partial class SettingModelFactory : ISettingModelFactory
    {
        private readonly ISettingService _settingService;
        private int storeId = 0;
        public SettingModelFactory(ISettingService settingService)
        {
            _settingService = settingService;
        }
        public GeneralCommonSettingsModel PrepareGeneralCommonSettingsModel()
        {
            var model = new GeneralCommonSettingsModel
            {
                ActiveStoreScopeConfiguration = 0
            };
            //prepare store information settings model
            model.StoreInformationSettings = PrepareStoreInformationSettingsModel();
            //prepare Sitemap settings model
            model.SitemapSettings = PrepareSitemapSettingsModel();
            //prepare SEO settings model
            model.SeoSettings = PrepareSeoSettingsModel();
            //prepare security settings model
            model.SecuritySettings = PrepareSecuritySettingsModel();
            return model;
        }
        private SecuritySettingsModel PrepareSecuritySettingsModel()
        {
            var securitySettings = _settingService.LoadSetting<SecuritySettings>(storeId);
            //fill in model values from the entity
            var model = new SecuritySettingsModel
            {
                EncryptionKey = securitySettings.EncryptionKey,
                ForceSslForAllPages = securitySettings.ForceSslForAllPages,
                EnableXsrfProtectionForAdminArea = securitySettings.EnableXsrfProtectionForAdminArea,
                HoneypotEnabled = securitySettings.HoneypotEnabled,
            };
            //fill in additional values (not existing in the entity)
            if (securitySettings.AdminAreaAllowedIpAddresses != null)
                model.AdminAreaAllowedIpAddresses = string.Join(",", securitySettings.AdminAreaAllowedIpAddresses);

            return model;
        }
        private SeoSettingsModel PrepareSeoSettingsModel()
        {
            var sitemapSettings = _settingService.LoadSetting<SeoSettings>(storeId);
            //fill in model values from the entity
            var model = new SeoSettingsModel
            {
                DefaultTitle = sitemapSettings.DefaultTitle,
                DefaultMetaKeywords = sitemapSettings.DefaultMetaKeywords,
                DefaultMetaDescription = sitemapSettings.DefaultMetaDescription,
            };
            return model;
        }
        private SitemapSettingsModel PrepareSitemapSettingsModel()
        {
            var sitemapSettings = _settingService.LoadSetting<SitemapSettings>(storeId);
            //fill in model values from the entity
            var model = new SitemapSettingsModel
            {
                SitemapEnabled = sitemapSettings.SitemapEnabled,
            };
            return model;
        }
        private StoreInformationSettingsModel PrepareStoreInformationSettingsModel()
        {
            var storeId = 0;
            var storeInformationSettings = _settingService.LoadSetting<StoreInformationSettings>(storeId);
            var commonSettings = _settingService.LoadSetting<CommonSettings>(storeId);

            //fill in model values from the entity
            var model = new StoreInformationSettingsModel
            {
                StoreClosed = storeInformationSettings.StoreClosed,
                DefaultStoreTheme = storeInformationSettings.DefaultStoreTheme,
                LogoPictureId = storeInformationSettings.LogoPictureId,
                SubjectFieldOnContactUsForm = commonSettings.SubjectFieldOnContactUsForm,
                UseSystemEmailForContactUsForm = commonSettings.UseSystemEmailForContactUsForm
            };
            return model;
        }
    }
}
